﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using TestVisioCanvas.Wrappers;

namespace TestVisioCanvas.ApplicationState
{
    public class PageState
    {
        // maintains the informations about all the shapes present the page
        private ObservableCollection<IShape> _pageContent;
        private ObservableCollection<Models.InvoiceModel> _invoices;
        private int _pageID;
        private string _pageName;

        public string PageName
        {
            get { return _pageName; }
            set { _pageName = value; }
        }

        public PageState(int PageID, string PageName)
        {
            _pageContent = new ObservableCollection<IShape>();
            _invoices = new ObservableCollection<Models.InvoiceModel>();
            _pageID = PageID;
            _pageName = PageName;
        }

        public ObservableCollection<IShape> Shapes
        {
            get { return _pageContent; }
        }

        public void AddShape(IShape ShapeToAdd)
        {
            Shapes.Add(ShapeToAdd);
            if (ShapeToAdd is SellableShape)
                this.AddShapeToInvoice(ShapeToAdd as SellableShape);
        }

        public void RemoveShape(IShape ShapeToRemove)
        {
            Shapes.Remove(ShapeToRemove);
            if (ShapeToRemove is SellableShape)
                this.RemoveShapeFromInvoice(ShapeToRemove as SellableShape);
        }

        public int PageID
        {
            get { return _pageID; }
        }

        #region INVOICES MANAGEMENT
        /// <summary>
        /// Collection of the invoices
        /// </summary>
        public ObservableCollection<Models.InvoiceModel> SelectedProducts
        {
            get { return _invoices; }
        }

        /// <summary>
        /// Total cost of the invoice
        /// </summary>
        public double TotalCost
        {
            get { return _invoices.Sum(x => x.TotalCost); }
        }

        /// <summary>
        /// Method that adds a product on the invoice.
        /// </summary>
        /// <param name="item">The product to add</param>
        private void AddShapeToInvoice(Wrappers.SellableShape item)
        {
            // tries to find the product inside the invoice.
            var p =
                _invoices.FirstOrDefault(x => 
                    x.Product.ProductDetails.PSMNTProductCode == item.ProductDetails.PSMNTProductCode);
            // if the product is already present on the invoice it updates the quantity
            if (p != null)
            {
                p.Quantity++;
            }
            // if not, the product "record" is added to the invoice
            else
            {
                _invoices.Add(new Models.InvoiceModel(item));
            }
        }

        /// <summary>
        /// Method that removes a product from the invoice
        /// </summary>
        /// <param name="item">The product to remove</param>
        private void RemoveShapeFromInvoice(Wrappers.SellableShape item)
        {
            // tries to find the product inside the invoice.
            var p =
                _invoices.FirstOrDefault(x => x.Product.ProductDetails.PSMNTProductCode == item.ProductDetails.PSMNTProductCode);
            // if the product is present
            if (p != null)
            {
                // updates its quantity
                p.Quantity--;
                // if quantity equals 0, the product "record" is removed from the invoice
                if (p.Quantity == 0)
                {
                    _invoices.Remove(p);
                }
            }
        }
        #endregion

    }
}
